// 获取应用实例
const app = getApp();

Page({
  data: {
    statistics: {
      totalCount: 0,
      reviewedCount: 0,
      masteredCount: 0
    },
    wrongList: []
  },

  onLoad() {
    this.loadPageData();
  },

  onPullDownRefresh() {
    this.loadPageData().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  // 加载页面数据
  loadPageData() {
    return Promise.all([
      this.loadStatistics(),
      this.loadWrongList()
    ]);
  },

  // 加载统计信息
  loadStatistics() {
    return app.request({
      url: '/wrong-book/statistics',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          statistics: res.data
        });
      }
    }).catch(() => {
      app.showError('获取统计信息失败');
    });
  },

  // 加载错题列表
  loadWrongList() {
    app.showLoading();
    return app.request({
      url: '/wrong-book/list',
      method: 'GET'
    }).then(res => {
      app.hideLoading();
      if (res.code === 0) {
        this.setData({
          wrongList: res.data.list
        });
      }
    }).catch(() => {
      app.hideLoading();
      app.showError('获取错题列表失败');
    });
  },

  // 跳转到题目详情
  goToQuestion(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/wrong-book/detail/detail?id=${id}`
    });
  }
}); 